package com.cashmama.market.api.application.controller.authentication.email;

import com.cashmama.market.api.application.controller.authentication.AuthenticationBaseHandler;
import com.cashmama.market.api.application.controller.authentication.AuthenticationController;
import com.cashmama.market.api.application.controller.authentication.personalInfo.InputContent;
import com.cashmama.market.api.application.controller.authentication.response.UserProfileResponse;
import com.cashmama.market.api.application.controller.authentication.vos.AuthenticationContentVo;
import com.cashmama.market.api.application.controller.authentication.vos.InputParams;
import com.cashmama.market.api.application.enums.AuthenticationTypeEnum;
import com.cashmama.market.api.application.enums.ParamType;
import com.cashmama.market.api.domain.service.ApiUserService;
import com.cashmama.market.api.domain.vo.RequestLocalInfo;
import com.cashmama.market.api.infras.enums.BasicErrorCodeEnum;
import com.cashmama.market.api.infras.enums.JumpRouterEnum;
import com.cashmama.market.api.infras.exception.ApplicationException;
import com.cashmama.market.api.infras.utils.StringUtils;
import com.fqgj.xjd.user.client.UserEmailService;
import com.fqgj.xjd.user.client.enums.AuthStatusEnum;
import com.fqgj.xjd.user.client.response.UserAuthEmail;
import com.qianli.user.enums.UserAuthEnum;
import com.qianli.user.enums.UserAuthStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: yujianfu (mr.vencnet@gmail.com)
 * Date: 2019-12-23
 * Time: 09:56
 */
@Service
public class EmailAuthenticationHandler extends AuthenticationBaseHandler {

    @Autowired
    private UserEmailService userEmailService;
    @Autowired
    private ApiUserService apiUserService;

    @PostConstruct
    public void init() {
        AuthenticationController.authenticationBaseHandlerMap.put(AuthenticationTypeEnum.EMAIL.getType(), this);
    }

    @Override
    public AuthenticationContentVo content() {

        Email email = new Email();
        return explain(email);
    }

    @Override
    public UserProfileResponse save(Map<String, String> inputParams) {
        Email email = new Email();
        getObject(email, inputParams);
        doSave(email);
        UserProfileResponse userProfileResponse = new UserProfileResponse()
                .setJumpUrl(JumpRouterEnum.PERSONAL_INFO_BANK.getJumpUrl());
        return userProfileResponse;
    }

    private class Email {
        @InputContent(paramName = "Email", param = "email", paramType = ParamType.TEXT, inputDesc = "Enter Your Valid Email Address")
        private InputParams email;

        public InputParams getEmail() {
            return email;
        }

        public Email setEmail(InputParams email) {
            this.email = email;
            return this;
        }
    }

    private void doSave(Email email){
        if(StringUtils.isEmpty(email.getEmail().getInputValue())){
            throw new ApplicationException("email can not be empty");
        }
        String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        UserAuthEmail userAuthEmail = new UserAuthEmail()
                .setEmail(email.getEmail().getInputValue())
                .setUserCode(userCode)
                .setStatus(AuthStatusEnum.AUTH_SUCCESS.getStatus());
        userEmailService.addUserEmail(userAuthEmail);
        apiUserService.updateUserAuthStatus(UserAuthStatusEnum.SUCCESS, UserAuthEnum.EMAIL_STATUS, RequestLocalInfo.getCurrentUser().getUserCode());
    }



}
